package jax.io;

/**
 * ...
 * @author tom
 */
class JInputStream implements jax.io.JCloseable {
	var _ntv:haxe.io.Input;
	var _mark:haxe.io.Bytes;
	public function new (?ntv:haxe.io.Input) {
		_ntv = ntv;
	}
	public function availiable() {
		var bytes = _ntv.readAll();
		_ntv = new haxe.io.BytesInput(bytes);
		return bytes.length;
	}
	public function read(?b:Array<Int>, ?off:haxe.Int32, ?len:haxe.Int32) {
		
		if(b == null)
			return _ntv.readByte();
		else {
			for(i in haxe.Int32.toInt(off)...haxe.Int32.toInt(len)) {
				b[i] = _ntv.readByte();
			}
			return b.length;
		}
	}
	public function skip(n:haxe.Int64):haxe.Int64 {
		while(!haxe.Int64.isNeg(n)) {
			_ntv.readByte();
			n = haxe.Int64.sub(n, haxe.Int64.ofInt(1));
		}
		return n;
	}
	public function close() {
		_ntv.close();
	}
	public function mark() {
		_mark = _ntv.readAll();
		reset();
	}
	public function markSupported():Bool {
		return true;
	}
	public function reset() {
		_ntv.close();
		_ntv = new haxe.io.BytesInput(_mark);
	}
}
